package com.example.score.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.score.entity.QuestionInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Mapper
public interface QuestionInfoMapper extends BaseMapper<QuestionInfo> {
    
    /**
     * 批量插入题目信息
     */
    @Insert("<script>" +
            "INSERT INTO t_question_info (exam_detail_id, question_id,question_bank_id, question_num, question_alias, question_type," +
            " question_type_name, question_answer, question_score, parent_id, created_time, updated_time,is_subjective_item" +
            " ,x,y,w,h,page) VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.examDetailId}, #{item.questionId},#{item.questionBankId}, #{item.questionNum}, #{item.questionAlias}, #{item.questionType}, " +
            "#{item.questionTypeName}, #{item.questionAnswer}, #{item.questionScore}, #{item.parentId}, " +
            "now(), now(),#{item.isSubjectiveItem},#{item.x},#{item.y},#{item.w},#{item.h},#{item.page})" +
            "</foreach>" +
            "</script>")
    void insertBatch(@Param("list") List<QuestionInfo> questions);

    /**
     * 根据考试详情ID查询题目信息
     */
    @Select("SELECT * FROM t_question_info WHERE exam_detail_id = #{examDetailId}")
    List<QuestionInfo> selectByExamDetailId(@Param("examDetailId") Long examDetailId);

    // 如果需要自定义批量查询方法
    @Select("<script>" +
            "SELECT * FROM t_question_info WHERE question_id IN " +
            "<foreach collection='questionIds' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            " AND exam_detail_id = #{examDetailId}" +
            "</script>")
    List<QuestionInfo> selectByQuestionIds(
        @Param("questionIds") List<Long> questionIds,
        @Param("examDetailId") Long examDetailId
    );

    @Select("SELECT qi.* FROM t_question_info qi " +
            "LEFT JOIN t_exam_detail ed ON qi.exam_detail_id = ed.id " +
            "WHERE qi.question_id = #{questionId}")
    QuestionInfo selectByQuestionId(@Param("questionId") Long questionId);

    /**
     * 根据考试ID查询题目信息
     */
    @Select("SELECT * FROM t_question_info WHERE exam_detail_id = #{examId}")
    List<QuestionInfo> selectByExamId(@Param("examId") Long examId);

    @Select("<script>select * from t_question_info qi where question_id in " +
            "<foreach collection='questionIds' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach> </script>")
    List<QuestionInfo> selectListByQuestionIds(List<Long> questionIds);

    @Select("<script>select exam_detail_id as examDetailId, count(1) as questionNumber from t_question_info qi where exam_detail_id in " +
            "<foreach collection='examDetailIds' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach> group by exam_detail_id </script>")
    List<Map<String, Object>> selectByExamDetailIds(List<Long> examDetailIds);
}